package m3;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;


import java.util.Scanner;

public class Producer {
    public static void main(String[] args) throws Exception {

        /*创建连接*/
        ConnectionFactory factory = new ConnectionFactory();
        //factory.setHost("192.168.64.140");
        factory.setHost("wht6.cn");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        /*创建 fanout 类型交换机 :logs*/
        //channel.exchangeDeclare("logs", "fanout");
        channel.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);

        /*向交换机发送消息*/
        while (true) {
            System.out.println("输入消息:");
            String s = new Scanner(System.in).nextLine();

            /* 对于fanout交换机, 第二个参数(指定队列名)无效*/
            channel.basicPublish("logs", "", null, s.getBytes());

        }
    }
}
